<?php
/**********************文件、图片、视频等快速处理功能性工具******************************/
/**
 * [checkStringIsBase64 description]
 * @Description 判断是否是base64图片
 * @Author      FengPQ
 * @DateTime    2020-05-14T08:39:39+0800
 * @param       [type]                   $str [图片地址]
 * @return      [type]                        [description]
 */
function checkStringIsBase64($str)
{
    if (strpos($str, 'data:image') === 0) {
        return true;
    }
    return false;
}

/**
 * [uploadImg description]
 * @Description 上传照片
 * @Author      FengPQ
 * @DateTime    2020-07-27T22:53:10+0800
 * @param       [type]                   $base64_image_content [description]
 * @param       [type]                   $name                 [description]
 * @param       boolean                  $file                 [description]
 * @return      [type]                                         [description]
 */
function uploadImg($base64_image_content, $name, $file = false)
{
    $director = "upload/" . $name . "/";
    //匹配出图片的格式
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
        $type     = $result[2];
        $new_file = $director . "" . date('Ymd', time()) . "/";
        if (!file_exists($new_file)) {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($new_file, 0777);
        }
        if (!$file) {
            $new_file = $new_file . (time() . getRandStr(8)) . "." . $type . "";
        } else {
            $new_file = $new_file . $file . "." . $type . "";
        }
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
            return retArray($file . "." . $type, true, $new_file);
        }
    }
    return retArray("图片上传失败.", false);
}

/**
 * [uploadVideo description]
 * @Description 上传视频
 * @Author      FengPQ
 * @DateTime    2020-07-27T22:53:27+0800
 * @param       [type]                   $base64_image_content [description]
 * @param       [type]                   $name                 [description]
 * @param       boolean                  $file                 [description]
 * @return      [type]                                         [description]
 */
function uploadVideo($base64_image_content, $name, $file = false)
{
    $director = "upload/" . $name . "/";
    //匹配出图片的格式
    if (preg_match('/^(data:\s*video\/(\w+);base64,)/', $base64_image_content, $result)) {
        $type     = $result[2];
        $new_file = $director . "" . date('Ymd', time()) . "/";
        if (!file_exists($new_file)) {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($new_file, 0777);
        }
        if (!$file) {
            $new_file = $new_file . (time() . getRandStr(8)) . "." . $type . "";
        } else {
            $new_file = $new_file . $file . "." . $type . "";
        }
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))) {
            return retArray($file . "." . $type, true, $new_file);
        }
    }
    return retArray("视频上传失败.", false);
}

/**
 * [Img description]
 * @Description 图片压缩
 * @Author      FengPQ
 * @DateTime    2020-07-27T22:54:03+0800
 * @param       [type]                   $Image [description]
 * @param       integer                  $Dw    [description]
 * @param       integer                  $Dh    [description]
 * @param       integer                  $Type  [description]
 */
function Img($Image, $Dw = 640, $Dh = 700, $Type = 1)
{
    if (!File_Exists($Image)) {
        return false;
    }
    //如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值
    if ($Type != 1) {
        Copy($Image, Str_Replace(".", "_x.", $Image));
        $Image = Str_Replace(".", "_x.", $Image);
    }
    //取得文件的类型,根据不同的类型建立不同的对象
    $ImgInfo = GetImageSize($Image);
    switch ($ImgInfo[2]) {
        case 1:
            $Img = @ImageCreateFromGIF($Image);
            break;
        case 2:
            $Img = @ImageCreateFromJPEG($Image);
            break;
        case 3:
            $Img = @ImageCreateFromPNG($Image);
            break;
    }
    //如果对象没有创建成功,则说明非图片文件
    if (empty($Img)) {
        //如果是生成缩略图的时候出错,则需要删掉已经复制的文件
        if ($Type != 1) {
            Unlink($Image);
        }
        return false;
    }
    //如果是执行调整尺寸操作则
    if ($Type == 1) {
        $w      = ImagesX($Img);
        $h      = ImagesY($Img);
        $width  = $w;
        $height = $h;
        if ($width > $Dw) {
            $Par    = $Dw / $width;
            $width  = $Dw;
            $height = $height * $Par;
            if ($height > $Dh) {
                $Par    = $Dh / $height;
                $height = $Dh;
                $width  = $width * $Par;
            }
        } elseif ($height > $Dh) {
            $Par    = $Dh / $height;
            $height = $Dh;
            $width  = $width * $Par;
            if ($width > $Dw) {
                $Par    = $Dw / $width;
                $width  = $Dw;
                $height = $height * $Par;
            }
        } else {
            $width  = $width;
            $height = $height;
        }
        $nImg = ImageCreateTrueColor($width, $height);
        //新建一个真彩色画布
        ImageCopyReSampled($nImg, $Img, 0, 0, 0, 0, $width, $height, $w, $h);
        //重采样拷贝部分图像并调整大小
        ImageJpeg($nImg, $Image);
        //以JPEG格式将图像输出到浏览器或文件
        return true;
        //如果是执行生成缩略图操作则
    } else {
        $w      = ImagesX($Img);
        $h      = ImagesY($Img);
        $width  = $w;
        $height = $h;
        $nImg   = ImageCreateTrueColor($Dw, $Dh);
        if ($h / $w > $Dh / $Dw) {
//高比较大
            $width  = $Dw;
            $height = $h * $Dw / $w;
            $IntNH  = $height - $Dh;
            ImageCopyReSampled($nImg, $Img, 0, -$IntNH / 1.8, 0, 0, $Dw, $height, $w, $h);
        } else {
//宽比较大
            $height = $Dh;
            $width  = $w * $Dh / $h;
            $IntNW  = $width - $Dw;
            ImageCopyReSampled($nImg, $Img, -$IntNW / 1.8, 0, 0, 0, $width, $Dh, $w, $h);
        }
        ImageJpeg($nImg, $Image);
        return true;
    }
}
